package cn.itcast.flink.trans;

import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

/**
 * Author itcast
 * Date 2022/1/13 11:09
 * Desc 将数据流输出到文件
 * DataStream 中 writeAsText 这个方法已过期，取而代之 FileStreamingSink 或 FileSink
 * 官方提供 Connector 连接器，用于将数据保存到本地/HDFS分布式文件系统上
 */
public class FileSinkDemo {
    public static void main(String[] args) throws Exception {
        //流环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //生成数据
        DataStreamSource<Integer> source = env.fromElements(1, 2, 3, 4, 5, 6, 7);
        //将数据流写入到 File 中
        source.writeAsText(
                "D:\\project\\flinkbase27\\data\\output\\num.txt",
                FileSystem.WriteMode.OVERWRITE
        );

        //执行环境
        env.execute();
    }
}
